/**
 * @copyright Copyright (c) 2025 CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author Xie Jinhui
 */

#include "main.h"

/* Define the software version string for identification and reporting purposes */
#define SOFTWARE_VERSION "2.00"
// #define SOFTWARE_VERSION  "wwan_iccid"

/**
 * @brief Display help information for WWAN command line interface
 * This function prints comprehensive usage instructions showing all available commands
 * and their proper syntax to assist users in operating the WWAN configuration tool
 */
static void help(void)
{
    /* Print the help message with detailed command usage instructions */
    printf("Please enter the correct parameters.\n\
wwan help\n\
wwan version\n\
wwan apn add {[carrier_name]}\n\
wwan apn show dev [port_name]\n\
wwan apn delete dev [port_name] [num]\n\
wwan apn delete dev [port_name] [carrier_name]\n\
wwan route add dev [port_name] [route_ip] [route_netmask]\n\
wwan route show dev [port_name]\n\
wwan route delete dev [port_name] [num]\n\
wwan route default set [port_name]\n\
wwan modem show dev [port_name]\n\
wwan at send [AT_cmd] dev [port_name]\n\
tip:  \n\
[carrier_name]: Mobile or Unicom or Telecom\n");
}

/**
 * @brief Parse and process command line options for WWAN configuration
 * This function serves as the main command dispatcher that interprets command line arguments
 * and routes them to appropriate handler functions based on the command structure
 * @param argc: Count of command line arguments passed to the program
 * @param argv: Array of character pointers listing all command line arguments
 * @return uint8_t Returns TRUE if command was processed successfully, otherwise shows help
 */
uint8_t getOptionParam(int argc, char **argv)
{
    /* Use switch statement to handle different argument counts for command routing */
    switch (argc)
    {
    /* Case for handling commands with exactly 2 arguments (program name + 1 command) */
    case 2:
        /* Check if the command is requesting software version information */
        if (!strcmp(argv[1], "version"))
        {
            /* Display the current software version to the user */
            printf("version: %s\n", SOFTWARE_VERSION);
        }
        else
        {
            /* Display help information when command is not recognized */
            help();
        }
        break;

    /* Case for handling commands with exactly 3 arguments (program name + 2 commands) */
    case 3:
        /* Check if command is to add APN without specifying carrier name */
        if (!strcmp(argv[1], "apn") && !strcmp(argv[2], "add"))
        {
            /* Execute function to add APN configuration with default settings */
            wwwan_apn_add();
        }
        // /* Commented out dialer start functionality for future implementation */
        // else if (!strcmp(argv[1], "dialer") && !strcmp(argv[2], "start"))
        // {
        //     wwwan_dialer_start();
        // }
        // /* Commented out dialer stop functionality for future implementation */
        // else if (!strcmp(argv[1], "dialer") && !strcmp(argv[2], "stop"))
        // {
        //     wwwan_dialer_stop();
        // }
        // /* Commented out modem reboot functionality for future implementation */
        // else if (!strcmp(argv[1], "modem") && !strcmp(argv[2], "reboot"))
        // {
        //     system("/etc/init.d/remotedial restart");
        // }
        // /* Commented out log start functionality for future implementation */
        // else if (!strcmp(argv[1], "log") && !strcmp(argv[2], "start"))
        // {
        //     wwwan_log_start();
        // }
        // /* Commented out log stop functionality for future implementation */
        // else if (!strcmp(argv[1], "log") && !strcmp(argv[2], "stop"))
        // {
        //     wwwan_log_stop();
        // }
        // /* Commented out AT command show functionality for future implementation */
        // else if (!strcmp(argv[1], "at") && !strcmp(argv[2], "show"))
        // {
        //     wwwan_log_show();
        // }
        else
        {
            /* Display help information when command combination is not recognized */
            help();
        }
        break;

    /* Case for handling commands with exactly 4 arguments (program name + 3 commands) */
    case 4:
        /* Check if command is to add APN with specific carrier name */
        if (!strcmp(argv[1], "apn") && !strcmp(argv[2], "add"))
        {
            /* Validate that the carrier name is one of the supported providers */
            if (!strcmp(argv[3], "Mobile") || !strcmp(argv[3], "Unicom") || !strcmp(argv[3], "Telecom"))
            {
                /* Copy the validated carrier name to the global APN message structure */
                strcpy(apnMsg.carrier_name, argv[3]);
                /* Execute function to add APN configuration for specified carrier */
                wwwan_apn_add();
            }
            else
            {
                /* Display help when carrier name is not one of the supported providers */
                help();
            }
        }
        else
        {
            /* Display help information when command combination is not recognized */
            help();
        }
        break;

    /* Case for handling commands with exactly 5 arguments (program name + 4 commands) */
    case 5:
        /* Check if command is to show APN configuration for specific device */
        if (!strcmp(argv[1], "apn") && !strcmp(argv[2], "show") && !strcmp(argv[3], "dev"))
        {
            /* Execute function to display APN settings for the specified port/device */
            wwan_apn_show(argv[4]);
        }
        /* Check if command is to show route configuration for specific device */
        else if (!strcmp(argv[1], "route") && !strcmp(argv[2], "show") && !strcmp(argv[3], "dev"))
        {
            /* Execute function to display routing table for the specified port/device */
            wwan_route_show(argv[4]);
        }
        /* Check if command is to set default route for specific device */
        else if (!strcmp(argv[1], "route") && !strcmp(argv[2], "default") && !strcmp(argv[3], "set"))
        {
            // /* Commented out network file creation for future implementation */
            // creat_PPP_network_file();
            /* Execute function to set default gateway route for specified port/device */
            wwan_route_defaultSet(argv[4]);
        }
        /* Check if command is to show modem information for specific device */
        else if (!strcmp(argv[1], "modem") && !strcmp(argv[2], "show") && !strcmp(argv[3], "dev"))
        {
            /* Execute function to display modem status and information for specified port */
            wwan_modem_showMsg(argv[4]);
        }
        else
        {
            /* Display help information when command combination is not recognized */
            help();
        }
        break;

    /* Case for handling commands with exactly 6 arguments (program name + 5 commands) */
    case 6:
        /* Check if command is to delete APN configuration for specific device */
        if (!strcmp(argv[1], "apn") && !strcmp(argv[2], "delete") && !strcmp(argv[3], "dev"))
        {
            /* Check if deletion is by carrier name (Mobile, Unicom, Telecom) */
            if (!strcmp(argv[5], "Mobile") || !strcmp(argv[5], "Unicom") || !strcmp(argv[5], "Telecom"))
            {
                /* Execute function to delete APN configuration by carrier name */
                if (!wwan_apn_delete_carrier(argv[4], argv[5]))
                {
                    /* Display error message if carrier-based APN deletion fails */
                    printf("ERROR: delete_one_carrier_apn error!\n");
                }
            }
            else 
            {
                /* Execute function to delete APN configuration by numerical index */
                if (!wwan_apn_delete_one(argv[4], argv[5]))
                {
                    /* Display error message if index-based APN deletion fails */
                    printf("ERROR: delete_one__apn error!\n");
                }
            }
        }
        /* Check if command is to delete route for specific device */
        else if (!strcmp(argv[1], "route") && !strcmp(argv[2], "delete") && !strcmp(argv[3], "dev"))
        {
            // /* Commented out network file creation for future implementation */
            // creat_PPP_network_file();
            /* Execute function to delete specific route from routing table */
            wwan_route_delete(argv[4], argv[5]);
        }
        /* Check if command is to send AT command to specific device */
        else if (!strcmp(argv[1], "at") && !strcmp(argv[2], "send") && !strcmp(argv[4], "dev"))
        {
            /* Execute function to send AT command to specified modem device */
            wwan_at_send(argv[3], argv[5]);
        }
        else
        {
            /* Display help information when command combination is not recognized */
            help();
        }
        break;

    /* Case for handling commands with exactly 7 arguments (program name + 6 commands) */
    case 7:
        /* Check if command is to add route for specific device with IP and netmask */
        if (!strcmp(argv[1], "route") && !strcmp(argv[2], "add") && !strcmp(argv[3], "dev"))
        {
            // /* Commented out network file creation for future implementation */
            // creat_PPP_network_file();
            /* Execute function to add new route with specified IP and subnet mask */
            wwan_route_add(argv[4], argv[5], argv[6]);
        }
        else
        {
            /* Display help information when command combination is not recognized */
            help();
        }
        break;

    /* Default case for handling any number of arguments not covered above */
    default:
        /* Always display help when argument count doesn't match any known command pattern */
        help();
        break;
    }

    /* Return TRUE to indicate successful command processing (help always counts as success) */
    return TRUE;
}